package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.CommentMessageDao;
import com.guochenglang.www.dao.LikeMessageDao;
import com.guochenglang.www.dao.ReplyMessageDao;
import com.guochenglang.www.pojo.CommentMessage;
import com.guochenglang.www.pojo.LikeMessage;
import com.guochenglang.www.pojo.ReplyMessage;
import com.guochenglang.www.service.MessageService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.MyDependency;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 消息提醒业务
 *
 * @author GuoChenglang
 */
public class MessageServiceImpl implements MessageService {
    private static final LikeMessageDao LIKE_MESSAGE_DAO = MyDependency.returnObject(LikeMessageDao.class);
    private static final CommentMessageDao COMMENT_MESSAGE_DAO = MyDependency.returnObject(CommentMessageDao.class);
    private static final ReplyMessageDao REPLY_MESSAGE_DAO = MyDependency.returnObject(ReplyMessageDao.class);

    /**
     * 获得点赞消息提醒
     *
     * @param userId 用户id
     * @return 点赞消息表
     */
    @Affair
    @Override
    public ArrayList<LikeMessage> showLikeMessage(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        LikeMessage set = new LikeMessage();
        set.setIsRead(1);
        LikeMessage myMessage = new LikeMessage();
        myMessage.setUserId(userId);
        //将未读变成已读
        LIKE_MESSAGE_DAO.updateLikeMessage(set, myMessage);
        //展示表
        return LIKE_MESSAGE_DAO.likeMessageSelect(new LikeMessage(null, null, userId, null));
    }

    /**
     * 未读点赞消息个数
     *
     * @param userId 用户id
     * @return 未读点赞消息个数
     */
    @Override
    public int likeMessageCount(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return LIKE_MESSAGE_DAO.likeMessageSelect(new LikeMessage(null, 0, userId, null)).size();
    }

    /**
     * 删除点赞消息
     *
     * @param messageId 信息的id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteLikeMessage(Integer messageId) throws SQLException, InterruptedException, IllegalAccessException {
        LikeMessage condition = new LikeMessage();
        condition.setId(messageId);
        return LIKE_MESSAGE_DAO.deleteLikeMessage(condition);
    }

    /**
     * 获得未读评论消息个数
     *
     * @param userId 用户id
     * @return 未读评论消息个数
     */
    @Override
    public int commentCount(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        CommentMessage condition = new CommentMessage();
        condition.setUserId(userId);
        condition.setIsRead(0);
        return COMMENT_MESSAGE_DAO.commentMessageSelect(condition).size();
    }

    /**
     * 获得评论消息列表
     *
     * @param userId 用户id
     * @return 评论消息列表
     */
    @Affair
    @Override
    public ArrayList<CommentMessage> showCommentMessage(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //未读变成已读
        CommentMessage set = new CommentMessage();
        set.setIsRead(1);
        CommentMessage condition = new CommentMessage();
        condition.setUserId(userId);
        COMMENT_MESSAGE_DAO.updateMessage(set, condition);
        return COMMENT_MESSAGE_DAO.commentMessageSelect(condition);
    }

    /**
     * 删除评论消息
     *
     * @param messageId 信息id
     * @return 删除行数
     * @throws SQLException           sql异常
     * @throws InterruptedException   断点异常
     * @throws IllegalAccessException 非法访问异常
     */
    @Affair
    @Override
    public int deleteCommentMessage(Integer messageId) throws SQLException, InterruptedException, IllegalAccessException {
        CommentMessage condition = new CommentMessage();
        condition.setId(messageId);
        return COMMENT_MESSAGE_DAO.deleteCommentMessage(condition);
    }

    /**
     * 获得回复消息个数
     *
     * @param userId 用户id
     * @return 回复消息个数
     */
    @Override
    public int replyCount(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        ReplyMessage condition = new ReplyMessage();
        condition.setIsRead(0);
        condition.setUserId(userId);
        return REPLY_MESSAGE_DAO.replyMessageSelect(condition).size();
    }

    /**
     * 获得回复雄安锡列表
     *
     * @param userId 用户id
     * @return 获得回复消息列表
     */
    @Override
    public ArrayList<ReplyMessage> showReplyMessage(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //未读变成已读
        ReplyMessage set = new ReplyMessage();
        set.setIsRead(1);
        ReplyMessage condition = new ReplyMessage();
        condition.setUserId(userId);
        REPLY_MESSAGE_DAO.updateReplyMessage(set, condition);
        //返回回复消息列表
        return REPLY_MESSAGE_DAO.replyMessageSelect(condition);
    }

    /**
     * 删除消息
     *
     * @param messageId 消息id
     * @return 删除行数
     */
    @Override
    public int deleteReplyMessage(Integer messageId) throws SQLException, InterruptedException, IllegalAccessException {
        ReplyMessage condition = new ReplyMessage();
        condition.setId(messageId);
        return REPLY_MESSAGE_DAO.deleteReplyMessage(condition);
    }

}
